package com.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.RewardDao;
import com.dao.SalaryMapper;
import com.dao.UserDao;
import com.pojo.Reward;
import com.pojo.User;

@Controller
public class PageController {
	@Resource
	private RewardDao rwDao;
	@Resource
	private UserDao uDao;

	@Autowired
	SalaryMapper salaryMapper;

	@RequestMapping(value = "reward")
	public String reward(Model model) {
		System.out.println("I am coming!!!");
		List<Reward> list = rwDao.getAll();
		model.addAttribute("userlist", list);
		return "reward";
	}

	@ResponseBody
	@RequestMapping(value = "delReward", method = RequestMethod.POST)
	public String delReward(int id) {
		Reward reward=rwDao.selectByPrimary(id);
		double salary=reward.getRw_salary();
		if (reward.getRw_type()==0){
			salaryMapper.updateReward(reward.getU_id(), salary);
		}else {
			salary=0-salary;
			salaryMapper.updateReward(reward.getU_id(), salary);
		}
		int result = rwDao.delReward(id);
		System.out.println("result:" + result);
		if (result == 1) {
			return "success";
		}
		return "fail";
	}

	@ResponseBody
	@RequestMapping(value = "addReward", method = RequestMethod.POST)
	public String addReward(String nickname, int type, String content, double salary) {
		System.out.println("进入addReward" + nickname);
		User user = uDao.getByNickname(nickname);
		System.out.println(user);
		if (user == null) {
			return "not nickname";
		}
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		System.out.println(time);
		int result = rwDao.addReward(new Reward(user.getU_id(), nickname, time, content, salary, type));
		if (type==0){
			salary=0-salary;
			salaryMapper.updateReward(user.getU_id(), salary);
		}else {
			salaryMapper.updateReward(user.getU_id(), salary);
		}
		System.out.println("result:" + result);
		if (result == 1) {
			return "success";
		}
		return "fail";
	}

	@ResponseBody
	@RequestMapping(value = "updateReward", method = RequestMethod.POST)
	public String updateReward(Reward reward) {
		System.out.println(reward);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		System.out.println(time);
		reward.setRw_date(time);
		Reward reward1=rwDao.selectByPrimary(reward.getRw_id());
		double salary=reward1.getRw_salary();
		if (reward1.getRw_type()==0&&reward.getRw_type()==0){
			salary=-salary-(-reward.getRw_salary());
			salaryMapper.updateReward(reward.getU_id(), salary);
		}else if (reward1.getRw_type()==1&&reward.getRw_type()==0){
			salary=salary-(-reward.getRw_salary());
			salaryMapper.updateReward(reward.getU_id(), salary);
		}else if (reward1.getRw_type()==1&&reward.getRw_type()==1){
			salary=salary-(reward.getRw_salary());
			salaryMapper.updateReward(reward.getU_id(), salary);
		}
		if (reward1.getRw_type()==0&&reward.getRw_type()==1){
			salary=-salary-(reward.getRw_salary());
			salaryMapper.updateReward(reward.getU_id(), salary);
		}
		int result = rwDao.updateReward(reward);

		System.out.println("result:" + result);
		if (result == 1) {
			return "success";
		}
		return "fail";
	}

	@RequestMapping("find")
	public String findNull(Model model) {
		List<Reward> list = rwDao.getAll();
		model.addAttribute("userlist", list);
		return "reward";
	}

	@RequestMapping(value = "/find/{nickname}")
	public String find(Model model, @PathVariable String nickname) {
		System.out.println("nickname:" + nickname);
		List<Reward> list = rwDao.selectAllByNickname(nickname);
		model.addAttribute("userlist", list);
		return "reward";
	}

	@ResponseBody
	@RequestMapping("test")
	public boolean test() {
		Subject subject = SecurityUtils.getSubject();
		boolean flag = subject.hasRole("admin");
		return flag;
	}
}
